<template>
  <el-dialog title="人员选择" :close-on-click-modal="false" :visible.sync="visible" append-to-body
             @close="clearDataList()" width="90vw" top="5vh">
    <el-container>
      <el-main style="height:65vh;overflow-x: hidden;">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="24" :md="7">
            <div class="boxsty" style="height: 64vh;overflow: auto;">
              <el-tree :data="treeData" :props="defaultProps" ref="deptTree" :filter-node-method="filterNode" class="filter-tree"
                       node-key="id" highlight-current :default-expanded-keys="[1]" accordion
                       @node-click="getNodeData"/>
            </div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="7">
            <div class="boxsty" style="height: 64vh">
              <div class="flex titbottom">
                <el-input v-model="listQuery.name" placeholder="请输入搜索用户姓名" style="margin-right:10px"></el-input>
                <el-button @click="btnSearch" type="primary" icon="el-icon-search" plain>搜索</el-button>
              </div>
              <el-table @row-click="handleClick" :data="tableDataDept" v-loading="tableloading" style="width: 100%;"
                        height="70%" border>
                <el-table-column prop="ryCode" label="用户">
                </el-table-column>
                <el-table-column prop="ryXm" label="姓名">
                </el-table-column>
              </el-table>
              <div style="margin-top:20px">
                <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
                               :current-page="page.currentPage" :page-sizes="[10, 20, 50, 100]"
                               :page-size="page.pageSize"
                               :total="page.total" background layout="total, sizes, prev, pager, next">
                </el-pagination>
              </div>
            </div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="10">
            <div class="boxsty" style="height: 64vh">
              <div class="tabletext titbottom">已选择人员</div>
              <el-table :data="tableDataUser" ref="ry-table" stripe height="90%" border>
                <el-table-column prop="ryName" label="姓名">
                </el-table-column>
                <!--                <el-table-column prop="deptName" label="部门" min-width="100">-->
                <!--                </el-table-column>-->
                <el-table-column  label="操作" width="100">
                  <template slot-scope="scope">
                    <el-button @click="delUserList(scope.$index)" type="danger" plain>删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="canSubmit" @click="btnSave">确定</el-button>
      <el-button @click="btnCancel" type="primary" plain>取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {fetchTree} from '@/api/admin/dept'
import {ControlPage} from '@/api/jiangjin/jiangjinryxxb'
import {addRyfzmxbList, getFzList, getOtherRyfzmx} from '@/api/jiangjin/jiangjinryfzmxb'
import {getObj} from '@/api/admin/dept'


export default {
  data() {
    return {
      qjDeptId: 0, //全局部门id
      mrformData: {},
      tableloading: false, //table遮挡
      visible: false,
      canSubmit: false,
      tableDataDept: [],
      tableDataUser: [],
      listQuery: {
        name: ''
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false // 是否倒序
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    init(data) {
      if(this.$refs.deptTree && this.$refs.deptTree.store.currentNode){
          this.qjDeptId = this.$refs.deptTree.store.currentNode.key;
          this.getPageUsers();
      }
      this.mrformData = JSON.parse(JSON.stringify(data));
      this.visible = true;
      this.canSubmit = true;
      this.loadTableDataUser(data.id);
    },
    //查询已选择对象
    loadTableDataUser(fzId) {
      getFzList(fzId).then(data => {
        this.tableDataUser = data.data.data;
      }).catch(() => {
        this.$notify.error('数据错误')
      });
    },
    getList() {
      fetchTree(this.listQuery).then(response => {
        this.treeData = response.data.data
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getPersons(page, params) {
      this.tableloading = true;

      ControlPage(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      },params)).then(response => {
        this.tableDataDept = response.data.data.records
        this.page.total = response.data.data.total
        this.tableloading = false;
      }).catch(err => {
        this.tableloading = false;
      })
    },
    getNodeData(data) {
      this.page.currentPage = 1
      this.qjDeptId = data.id;

      this.getPersons(this.page, {
        rySjdwId: data.id,
        ryCode:''
      })
    },
    //选人添加到右边的框
    //增加判断
    handleClick(vm) {
      for (var i = 0; i < this.tableDataUser.length; i++) {
        if (this.tableDataUser[i].ryCode == vm.ryCode) {
          return;
        }
      }
      this.addRightRyxx(vm);
    },
    //添加人到右边
    addRightRyxx(vm){
      let objUser = {};
      objUser["jjDeptId"] = this.mrformData.jjDeptId;
      objUser["jjRyfzId"] = this.mrformData.id;
      objUser["ryCode"] = vm.ryCode;
      objUser["ryName"] = vm.ryXm;
      objUser["ryZw"] = vm.ryZhiwu;
      objUser["deptId"] = this.mrformData.jjDeptId;

      this.tableDataUser.push(objUser);
    },
    //删除人员list
    delUserList(index) {
      this.tableDataUser.splice(index, 1);
    },
    //清空记录
    clearDataList() {
      this.qjDeptId = '';
      this.tableDataDept = [];
      this.tableDataUser = [];
      this.listQuery.name = '';
    },
    //保存人员
    btnSave() {
      this.canSubmit = false;
      addRyfzmxbList(this.tableDataUser, this.mrformData.id).then(data => {
        this.$notify.closeAll();
        this.$notify.success('操作成功');
        this.visible = false;
        this.$emit('refresh');
      }).catch(() => {
        this.canSubmit = true;
      });
    },
    // tree搜索事件,搜索人
    btnSearch() {
      let param = {
        rySjdwId: this.qjDeptId
      }

      if (this.listQuery.name.trim() !== '') {
        // param['ryCode'] = this.listQuery.name.trim();
        param['ryCode'] = this.listQuery.name.trim();
      }

     //console.log(param);
      this.getPageUsers(param);
    },
    // 分页
    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.getPageUsers()
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val
      this.getPageUsers()
    },
    //分页刷新
    getPageUsers(vm) {
      if (vm) {
        this.getPersons(this.page, vm)
      } else {
        this.getPersons(this.page, {
          rySjdwId: this.qjDeptId,
          ryCode:''
        })
      }
    },
    //取消按钮
    btnCancel() {
      this.visible = false;
      this.clearDataList();
    },
  }
}

</script>

<style scoped>
</style>
